package session;

import entity.Category;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;


/**
 * Gestisce le transazioni con il DBMS per gli oggetti Category.
 *
 * @author Rachid Ouchary, Fabio Strocco, Max Passaghe, Marco Tansini.
 */
@Stateless
public class CategoryFacade implements CategoryFacadeRemote {
    @PersistenceContext(unitName = "WishLister-ejbPU")
    private EntityManager em;

    public void create(Category category) {
        em.persist(category);
    }

    public void edit(Category category) {
        em.merge(category);
    }

    public void remove(Category category) {
        em.remove(em.merge(category));
    }

    public Category find(Long id) {
        return em.find(Category.class, id);
    }

    public List<Category> findMainCategories() {
        Query q = em.createNamedQuery(Category.FIND_MAIN_CATEGORIES);
        List<Category> categoryList = q.getResultList();
        categoryList.size();
        for(Category subCategory : categoryList) {
            subCategory.getSottoCategorie().size();
        }
        return categoryList;
    }

    public List<Category> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Category.class));
        return em.createQuery(cq).getResultList();
    }

    public List<Category> findRange(int[] range) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Category.class));
        Query q = em.createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<Category> rt = cq.from(Category.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public String findCategoryVersion(){
        Query q = em.createQuery("SELECT c "
                               + "FROM   Category c "
                               + "WHERE  c.ebayID =:specialCategoryID");

        q.setParameter("specialCategoryID", entity.Category.VERSION_CATEGORY_ID);
        List result =  q.getResultList();

        if(result.isEmpty()) return null;
        else {
            Category res = (Category) result.get(0);
            return res.getCatName();
        }

    }

    /*
     * Restituisce una categoria con il corrispondente ebayID
     */
    public Category findCategoryById(String ebayId) {
        Query q = em.createQuery("SELECT c "
                               + "FROM   Category c "
                               + "WHERE  c.ebayID =:ebayId");

        q.setParameter("ebayId", ebayId);
        List result =  q.getResultList();

        if(result.isEmpty()) return null;
        else {
            return (Category) result.get(0);
        }

    }
}
